package com.mz.ozy.activity.me;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mz.ozy.R;
import com.mz.ozy.adapter.MySellKtAdapter;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.OrderListBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.presenter.MyBalancePresenter;
import com.mz.ozy.mvp.view.MyBalanceContract;
import com.mz.ozy.utils.Utils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author lifeStyle on 2020/4/2 0002.
 */
public class MybalanceActivity extends BaseActivity<MyBalanceContract.View, MyBalancePresenter> implements MyBalanceContract.View {
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.tv_balance)
    TextView tvBalance;

    private int pageNo = 1;
    private int tag = 0;
    private List<OrderListBean.PageBean.PageItemsBean> mBeanList = new ArrayList<>();
    private MySellKtAdapter mAdapter;
    private OrderListBean dataItem;
    private String remain;

    public static void go(Context context, String remain) {
        Intent intent = new Intent(context, MybalanceActivity.class);
        intent.putExtra("remain", remain);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_balance);
        ButterKnife.bind(this);
        initTitle();
        tvCenterTitle.setText("我的欧花");
        initView();
        remain = getIntent().getStringExtra("remain");
    }

    private void initView() {
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new MySellKtAdapter(mBeanList, 3);
        recyclerview.setAdapter(mAdapter);
        smartRefreshLayout.setEnableAutoLoadmore(true);
        smartRefreshLayout.autoRefresh();
        smartRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if (mPresenter == null) return;
                tag = 1;
                pageNo = pageNo + 1;
                mPresenter.getInfo(MybalanceActivity.this, pageNo + "", Constants.TransCode.TransCode10012);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                if (mPresenter == null) return;
                tag = 0;
                pageNo = 1;
                mPresenter.getInfo(MybalanceActivity.this, pageNo + "", Constants.TransCode.TransCode10012);
            }
        });


    }

    @Override
    protected void onResume() {
        super.onResume();
        pageNo = 1;
        mPresenter.getInfo(MybalanceActivity.this, pageNo + "", Constants.TransCode.TransCode10012);

    }

    @OnClick({R.id.tv_recharge, R.id.tv_exchange})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_recharge:
                MeRechargeActivity.go(this, dataItem.getUser_balance());
                // startActivity(new Intent(MybalanceActivity.this, MeRechargeActivity.class));
                break;
            case R.id.tv_exchange:
//                if (dataItem != null) {
                    Intent intent = new Intent(MybalanceActivity.this, ExchangeObActivity.class);
                    intent.putExtra("balance", remain);
                    startActivity(intent);
                    finish();
//                }
                break;
        }
    }

    @Override
    public void getBanclceInfo(OrderListBean bean) {
        dataItem = bean;
        tvBalance.setText(Utils.decimalUtils(bean.getUser_balance(), 2));
        if (tag == 0) {
            smartRefreshLayout.setLoadmoreFinished(false);
            smartRefreshLayout.finishRefresh();
            mBeanList.clear();
            if (bean.getPage().getPageItems().size() == 0) {
                llNoData.setVisibility(View.VISIBLE);
            } else {
                llNoData.setVisibility(View.GONE);
            }
        } else if (tag == 1) {
            smartRefreshLayout.finishLoadmore();
        }
        mBeanList.addAll(bean.getPage().getPageItems());
        mAdapter.notifyDataSetChanged();
        if (bean.getPage().getPageItems().size() == 0 || bean.getPage().getPageSize() * bean.getPage().getPageNumber() >= bean.getPage().getPageTotle()) {
            smartRefreshLayout.setLoadmoreFinished(true);//将不会再次触发加载更多事件
        }
    }
}
